<template>
  <div class="article-container">
    <van-nav-bar 
      class="app-nav-bar"
      title="文章详情"
      left-arrow
      @click-left="$router.back()"
    />
    <div class="article-warp">
      <h1 class="title">{{article.title}}</h1>
      <van-cell center class="user-info">
        <div slot="title" class="name">{{article.aut_name}}</div>
        <van-image 
          slot="icon"
          class="avatar"
          round
          fit="cover"
          :src="article.aut_photo"
        />
        <div slot="label" class="pubdate">{{article.pubdate | relativeTime}}</div>
        <van-button 
          class="follow-btn"
          :type="article.is_followed ? 'default': 'info'"
          :icon="article.is_followed ? '': 'plus'"
          size="small"
          round
          :loading="isFollowloading"
          @click="onFollow">{{article.is_followed ? '已关注' : '关注'}}</van-button>
      </van-cell>
          <!-- 内容区域 -->
      <div class="content markdown-body" 
          v-html="article.content"
          ref="article-content">
      </div>
          <!-- 评论列表 -->

      <comment-list :source="articleId"
                    :list="commentList"
                    @update-total-count="totalComment"
                    @reply-click="onReplyClick"/>
    </div>



    <!-- 底部tabar区域 -->
    <div class="article-bottom">
      <van-button 
        class="comment-btn"
        type="default"
        round
        size="small"
        @click="isPostShow = true"
      >写评论</van-button>
            <!-- 评论 -->
      <van-icon 
        name="comment-o"
        color="#777"
        :badge="totalCommentCount" 
        class="comment-icon"

      />
             <!-- 收藏 -->
      <van-icon 
        :color="article.is_collected ? 'orange' : '#777'" 
        :name="article.is_collected ? 'star' : 'star-o'"
        @click="onCollect"
      />
              <!-- 点赞 -->
      <van-icon 
        :color="article.attitude === 1 ? 'red' : '#777'" 
        :name="article.attitude === 1 ? 'good-job' :'good-job-o'"
        @click="onLike"/>
              <!-- 分享 -->
      <van-icon 
        color="#777" 
        name="share"/>
    </div>
    <van-popup 
      v-model="isPostShow"
      position="bottom"
    >      
            <!-- 评论弹出框 -->
      <post-comment :target="articleId"
                    @post-success="onPostSuccess">

      </post-comment>
    </van-popup>
        <!-- 回复弹出框 -->
        <van-popup 
          v-model="isReplyShow"
          position="bottom"
        >      
          <comment-reply v-if="isReplyShow"
                         :comment="replyComment"
                         @close="isReplyShow = false"
                         :article-id="articleId"
          />

        </van-popup>
  </div>
</template>

<script>
import './github-markdown.css'
import {getArticleById, 
        addCollect, 
        deleteCollect,
        addLike,
        deleteLike} from '@/api/article'
import { ImagePreview } from 'vant'
import {addFollow, deleteFollow} from '@/api/user'
import CommentList from './components/comment-list.vue'
import PostComment from './components/post-comment.vue'
import CommentReply from './components/comment-reply.vue'


export default {
  props:{
    articleId:{
      type:[String, Number, Object],
      required:true
  }
},
components:{
  CommentList,
  PostComment,
  CommentReply
},
  data() {
    return {
      article:{},
      isFollowloading:false,
      isCollectloading:false,
      isPostShow:false,
      commentList:[], //文章评论列表
      totalCommentCount:0,
      isReplyShow:false,
      replyComment:{}
    }
  },
  created() {
    this.loadArticle()
  },
  computed:{

  },
  methods: {
    async loadArticle(){
      const {data} =await getArticleById(this.articleId)
      this.article = data.data

      this.$nextTick(() => {
        this.handlePreviewImage()
      })
    },
    handlePreviewImage(){ //处理操作DOM
      const articleContent = this.$refs['article-content']
      const imgs = articleContent.querySelectorAll('img')
      const imgPaths = []
      imgs.forEach((img, index) => {
        imgPaths.push(img.src)
        img.onclick = function(){
          ImagePreview({
            images: imgPaths,
            startPosition: index
          })
        }
      })
    },
    async onFollow(){
      this.isFollowloading = true
      if (this.article.is_followed) {  //已关注，取消关注
        await deleteFollow(this.article.aut_id)
      }else{  //没有关注，添加关注
        await addFollow(this.article.aut_id)
      }
      this.article.is_followed = !this.article.is_followed
      this.isFollowloading = false
    },
    async onCollect(){
      this.isCollectloading = true
      if (this.article.is_collected) {  //已关注，取消关注
        await deleteCollect(this.articleId)
      }else{  //没有关注，添加关注
        await addCollect(this.articleId)
      }
      this.article.is_collected = !this.article.is_collected
      this.isCollectloading = false
      this.$toast.success(`${this.article.is_collected ? '' : '取消'}收藏成功`)
    },
    async onLike(){
      this.$toast.loading({
        message:'操作中...',
        forbidClick:true
      })
      if (this.article.attitude === 1) {
        await deleteLike(this.articleId)
        this.article.attitude = -1
      }else{
        await addLike(this.articleId)
        this.article.attitude = 1
      }
      this.$toast.success(`${this.article.attitude ===1 ? '' : '取消'}点赞成功`)
    },
    onPostSuccess(comment){

      this.commentList.unshift(comment)
      this.totalCommentCount++
      this.isPostShow = false
      
    },
    totalComment(total_count){
      this.totalCommentCount = total_count
    },
    onReplyClick(comment){
      console.log('onReplyClick', comment);
      this.replyComment = comment
      this.isReplyShow = true
    }
  },
}
</script>

<style lang="less" scoped>
.article-container{
  .title{
    font-size: 20px;
    color: #3a3a3a;
    padding: 15px;
    background-color: #fff;
    margin: 0;
  }
  .user-info{
    .name{
      font-size: 13px;
    }
    .pubdate{
      font-size: 13px;
      color: #b4b4b4;
    }
    .avatar{
      width: 35px;
      height: 35px;
      margin-right: 6px;
  }
    .follow-btn{
      width: 85px;
      height: 29px;
      font-size: 13px;
    }
  }
  ul{
    list-style: unset;
  }
  .content{
    padding: 14px;
    background-color: #fff;
  }
  .article-bottom{
    position: fixed;
    left: 0;
    bottom: 0;
    right: 0;
    box-sizing: border-box;
    display: flex;
    justify-content: space-around;
    align-items: center;
    height: 44px;
    border-top: 1px solid #d8d8d8;
    background-color: #fff;
    .comment-btn{
      width: 160px;
      height: 23px;
      border: 1px solid #eee;
      font-size: 15px;
      line-height: 23px;
      color: #a7a7a7;
    }
    .van-icon {
    font-size: 20px;
    }
    .comment-icon {
      bottom: -2px;
    }
    .share-icon {
      bottom: -2px;
    }
  }
  .article-warp{
    position: fixed;
    left: 0;
    right: 0;
    top: 46px;
    bottom: 44px;
    overflow-y:auto ;
  }
}
</style>